﻿using Siasun.AFC.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Siasun.AFC.AfcCommonLib.Base
{
    public class CommApi
    {
        /// <summary>
        /// String->Int
        /// </summary>
        /// <param name="IString">string字符串</param>
        /// <returns>int类型值</returns>
        public static int GetIntDataFromString(string IString)
        {
            if (string.IsNullOrEmpty(IString))
            {
                return -1;
            }
            int intDate = 0;
            if (int.TryParse(IString, out intDate))
            {
                return intDate;
            }
            return -1;
        }

        /// <summary>
        /// String->Double
        /// </summary>
        /// <param name="IString">double字符串</param>
        /// <returns>double值</returns>
        public static double GetDoubleDataFromString(string IString)
        {
            if (string.IsNullOrEmpty(IString))
            {
                return -1;
            }
            double lValue = 0;
            if (double.TryParse(IString, out lValue))
            {
                return lValue;
            }
            return -1;
        }

        /// <summary>
        /// String->Long
        /// </summary>
        /// <param name="lString">long字符串</param>
        /// <returns>long值</returns>
        public static long GetLongFromString(string lString)
        {
            if (string.IsNullOrEmpty(lString))
            {
                return 0;
            }
            long lValue = 0;
            if (long.TryParse(lString, out lValue))
            {
                return lValue;
            }
            return 0;
        }

        /// <summary>
        /// String->DateTime
        /// </summary>
        /// <param name="timeStr">字符串</param>
        /// <param name="format"> 时间格式</param>
        /// <returns>时间类型</returns>
        public static DateTime GetDateTimeFromString(string timeStr, string format)
        {
            DateTime dt = new DateTime();
            //IFormatProvider ifp = new System.Globalization.CultureInfo("zh-CN", true);
            if (DateTime.TryParseExact(timeStr, format, System.Globalization.CultureInfo.InvariantCulture,
                                    System.Globalization.DateTimeStyles.None, out dt))
            {
                return dt;
            }
            return DateTime.Now;
        }

        /// <summary>
        /// String->DateTime
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeFromString(string timeStr)
        {
            DateTime dt;
            if (DateTime.TryParse(timeStr, out dt))
            {
                return dt;
            }
            return DateTime.Now;
        }

        /// <summary>
        /// 16进制字符串转字节数组
        /// </summary>
        /// <param name="hexString">16进制字符串</param>
        /// <returns>返回字节数组</returns>
        public static List<byte> GetListDataFromHexString(string hexString)
        {
            try
            {
                if (string.IsNullOrEmpty(hexString))
                {
                    return null;
                }
                List<byte> bData = new List<byte>();
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                {
                    hexString += " ";
                }
                for (int i = 0; i < hexString.Length / 2; i++)
                {
                    bData.Add(Convert.ToByte(hexString.Substring(i * 2, 2), 16));
                }
                return bData;
            }
            catch (Exception e)
            {
                LoggerInf.Error("CommApi->getListDataFromHexString:16进制字符串转字节数组错误\n" + e.ToString());
            }
            return null;
        }

        /// <summary>
        /// 字符数组格式化为IP格式
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static string GetIpString(List<byte> ipData)
        {
            try
            {
                if (!object.Equals(null, ipData) && ipData.Count > 0)
                {
                    string ip = string.Empty;
                    foreach (byte buf in ipData)
                    {
                        ip += buf.ToString() + ".";
                    }
                    return ip.Remove(ip.Length - 1, 1);
                }
                return null;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }
    }
}
